let __result__ = {}
let __curr__ = {
    app_id: ThemeConfig.app_id,
    public_key: ThemeConfig.public_key,
    private_key: ThemeConfig.private_key,
}
ThemeConfig.app_id = "NaS8eb0af42eb09426395d9fe1d740a7932";
ThemeConfig.public_key = "NaSa13308ec2bcfb195a5a550c26a245fad";
ThemeConfig.private_key = "NaSc0df1eb3391a35ebc7e2ff1a4dbafa18";
$(".item-title").on("click", function () {
    if(typeof(parseInt(this.parentNode.getAttribute("value"))) !== "number")
        throw "[AliPay] 项目的金额必须是一个整数";
    let __bill__ = {
        name: this.parentNode.classList.value,
        amount: this.parentNode.getAttribute("value"),
    };
    let __action__ = new AlipayService(__bill__);
    __action__.doPay(this);
})
class AlipayService {
    constructor(__bill__) {
        this.$orderName = __bill__.name;
        this.$appId = __curr__.app_id;
        this.$payAmount = __bill__.amount;
        this.$rsaType = ThemeConfig.sign_type;
        this.$publicKey = __curr__.public_key;
        this.$privateKey = __curr__.private_key;
        this.$timeoutExpress = ThemeConfig.bill_timeout;
        this.$notifyUrl = this.doEmpty(ThemeConfig.notify_url);
    }
    doPay(dom) {
        if(this.isEmpty(this.$appId) || this.isEmpty(this.$publicKey) || this.isEmpty(this.$privateKey || this.isEmpty(this.$orderName)) || this.isEmpty(this.$payAmount)) {
            Qmsg.error("[AliPay]缺少参数");
            throw "[AliPay] 配置错误或遗漏参数";
        }
        this.uiUpgrade(dom);
        const that = this;
        let $config = {
            'total_amount': this.$payAmount,
            'subject': this.$orderName,
            'timeout_express': this.$timeoutExpress,
            'app_id': this.$appId,
            'sign_type': this.$rsaType,
            'privateKey': this.$privateKey,
            'publicKey': this.$publicKey,
            'notify_url': this.$notifyUrl,
        };
        $.ajax({
            type: "POST",
            url: "https://api.dioxide-cn.ink/alipay/api.handshake.php",
            data: {
                'config': $config
            },
            success:(res) => {
                let obj = JSON.parse(res);
                for (const key in obj) {
                    __result__[key] = obj[key];
                }
                that.showQR(obj);
                that.doCheck();
            },
            error:(err) => {
                Qmsg.error("[AliPay]参数错误，请检查配置");
                throw "[AliPay] Error occurred! Program has been stopped!";
            }
        });
    }
    doEmpty(obj) {
        if(obj === '' || obj === undefined || obj === null) {
            return ThemeConfig.blog_url;
        } else {
            if(obj.indexOf("http") === -1) {
                Qmsg.error("[AliPay]回调地址必须是一个可用的URL地址");
                throw "[AliPay] Error occurred! Program has been stopped!"
            } else {
                return obj.toString();
            }
        }
    }
    doCheck() {
        var __trigger__ = 0;
        var __check__ = setInterval(function() {
            __trigger__++;
            $.ajax({
                type: "POST",
                url: "https://api.dioxide-cn.ink/alipay/api.checkbill.php",
                data: {
                    'action': 'query',
                    'outTradeNo': __result__.out_trade_no,
                    'appid': __curr__.app_id,
                    'privateKey': __curr__.private_key
                },
                success:(res) => {
                    res = JSON.parse(res);
                    if(__trigger__ > parseInt(ThemeConfig.bill_timeout[0])*60) {
                        __trigger__ = undefined;
                        $('#__pay_state__').text('订单状态：超时关闭');
                        $('.alipay-pay-inner .qrcode').children()[1].setAttribute('src','https://api.qrserver.com/v1/create-qr-code?data=https://www.baidu.com/');
                        $('.mask').children()[0].setAttribute('class','fa fa-warning')
                        $('.mask').fadeIn(300);
                        clearInterval(__check__);
                    }
                    if(res.code === 200) {
                        __trigger__ = undefined;
                        $('#__pay_state__').text('订单状态：支付完成');
                        $('.alipay-pay-inner .qrcode').children()[1].setAttribute('src','https://api.qrserver.com/v1/create-qr-code?data=https://www.baidu.com/');
                        $('.mask').children()[0].setAttribute('class','fa fa-check')
                        $('.mask').fadeIn(300);
                        clearInterval(__check__);
                    }
                },
                error:(err) => {
                    Qmsg.error("[AliPay]获取订单错误");
                    throw "[AliPay] Error occurred! Program has been stopped!";
                }
            });
        }, 1000);
    }
    uiUpgrade(dom) {
        self = $(dom);
        self.fadeOut(300);
        self.parent().siblings().fadeOut(300);
        $(dom.parentNode.parentNode).fadeOut(300);
        $('.alipay-pay-progress').fadeIn(300);
    }
    showQR(obj) {
        $('.mask').fadeOut(300);
        let __img__ = 'https://api.qrserver.com/v1/create-qr-code?data='+obj.qr_code;
        $('.alipay-pay-inner .qrcode').children()[1].setAttribute('src',__img__);
        $('#__pay_order__').text('订单编号：'+obj.out_trade_no);
        $('#__pay_state__').text('订单状态：待支付...');
        $('#__pay_price__').text('订单金额：'+this.$payAmount+' ¥');
    }
    isEmpty(target) {
        if(target === undefined || target === null || target === '') {
            return true;
        } else {
            return false;
        }
    }
}

// article-swiper
$(window).resize(function() {
    let h = $(".article-swiper").width()*parseFloat($(".article-swiper").attr("scale"));
    $(".article-swiper").css("height",h+"px");
});